package com.undergroundbiology.undergroundbiology;


import android.app.ActionBar;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WebView myWebView = (WebView) findViewById(R.id.webView);
        myWebView.setWebViewClient(new WebViewClient());
        WebSettings webSettings = myWebView.getSettings();
        myWebView.getSettings().setBuiltInZoomControls(true);
        myWebView.getSettings().setAppCacheEnabled(true);
        myWebView.getSettings().setSaveFormData(true);
        myWebView.getSettings().setAllowFileAccess(true);
        webSettings.setJavaScriptEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayUseLogoEnabled(true);

        myWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT ); // Defaults to online loading
        if ( !isNetWorkAvailable() )  {
            myWebView.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ONLY );
        }


        myWebView.loadUrl("http://www.TrinovaLabs.com");

        CookieSyncManager.createInstance(this);
        CookieSyncManager.getInstance().startSync();
        CookieManager.getInstance();

    }



    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        WebView myWebView = (WebView) findViewById(R.id.webView);
        myWebView.setWebViewClient(new WebViewClient());


        if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
            myWebView.goBack();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);

        return super.onCreateOptionsMenu(menu);

    }



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {

            case R.id.refresh_settings:
                WebView myWebView = (WebView) findViewById(R.id.webView);
                myWebView.setWebViewClient(new WebViewClient());
                myWebView.reload();
                Toast.makeText(this, R.string.status_refreshing, Toast.LENGTH_SHORT)
                        .show();
                return true;

            case R.id.menu_share:
                Intent shareIntent = new Intent(Intent.ACTION_SEND);
                shareIntent.setType("text/plain");
                WebView shareURL = (WebView) findViewById(R.id.webView);
                String shareBody = shareURL.getUrl();
                shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Underground Biology");
                shareIntent.putExtra(Intent.EXTRA_TEXT, shareBody);
                startActivity(Intent.createChooser(shareIntent, "Share Via"));
                return true;

            case R.id.action_search:
                AlertDialog.Builder alert = new AlertDialog.Builder(this);

                alert.setTitle("Search");
                final EditText search_input = new EditText(this);
                alert.setView(search_input);

                alert.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        String search_input_2 = search_input.getText().toString();
                        String searchConvert = TextUtils.htmlEncode(search_input_2);
                        WebView searchWebView = (WebView) findViewById(R.id.webView);
                        showToastSearching();
                        searchWebView.loadUrl("http://undergroundbiology.com/?s=" + searchConvert);

                    }
                });

                alert.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // Cancel the Search Dialog.
                    }
                });

                alert.show();
                return true;

             case android.R.id.home:
                 Intent homeIntent = new Intent (this, MainMenu.class);
                 startActivity(homeIntent);
                return true;
            default:
            return super.onOptionsItemSelected(item);
        }
    }


    // ShowToast for Searching
    public void showToastSearching() {
        Toast.makeText(this,R.string.status_searching, Toast.LENGTH_SHORT).show();
    }

    // Check for network connection
    private boolean isNetWorkAvailable() {
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService( CONNECTIVITY_SERVICE );
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }


}
